﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web;

namespace API.Core
{
    public class Mail
    {
        private string SMTP_SERVER_HOST { get; set; }
        private int SMTP_SERVER_PORT { get; set; }
        private string SMTP_LOGIN { get; set; }
        private string SMTP_PASSWORD { get; set; }
        private bool SMTP_USE_SSL { get; set; }

        public Mail(string SMTP_SERVER_HOST, int SMTP_SERVER_PORT, string SMTP_LOGIN, string SMTP_PASSWORD, bool SMTP_USE_SSL)
        {
            this.SMTP_SERVER_HOST = SMTP_SERVER_HOST;
            this.SMTP_SERVER_PORT = SMTP_SERVER_PORT;
            this.SMTP_LOGIN = SMTP_LOGIN;
            this.SMTP_PASSWORD = SMTP_PASSWORD;
            this.SMTP_USE_SSL = SMTP_USE_SSL;
        }

        public void Send(MailItem item)
        {
            SmtpClient client = new SmtpClient(SMTP_SERVER_HOST, SMTP_SERVER_PORT);
            client.Credentials = new NetworkCredential(SMTP_LOGIN, SMTP_PASSWORD);
            client.EnableSsl = SMTP_USE_SSL;
            client.Timeout = 10000; //10 sec

            MailMessage message = new MailMessage();

            message.From = new MailAddress(item.From);

            if (item.To != null)
                item.To.Split(new char[] { ';', ',', ' ' }).ToList().ForEach(r => { if (r.Length != 0) message.To.Add(r); });

            if (item.CC != null)
                item.CC.Split(new char[] { ';', ',', ' ' }).ToList().ForEach(r => { if (r.Length != 0) message.CC.Add(r); });

            if (item.Bcc != null)
                item.Bcc.Split(new char[] { ';', ',', ' ' }).ToList().ForEach(r => { if (r.Length != 0) message.Bcc.Add(r); });

            message.Subject = item.Subject;
            message.Body = item.Body;

            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = false;

            //client.SendCompleted += client_SendCompleted;
            client.Send(message);
        }

        void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
                throw new Exception("Ошибка отправки сообщения", e.Error);

            /// e.UserState
            /// e.Error
            /// e.Cancelled
        }

        public class MailItem
        {
            public string From { get;set;} 
            public string To { get; set;}
            public string CC { get; set; }
            public string Bcc { get; set; }
            public string Subject{ get;set;}
            public string Body { get; set; }

            public MailItem() { }
            public MailItem(string from, string to, string subject, string body)
            {
                this.From = from;
                this.To = to;
                this.Subject = subject;
                this.Body = body;
            }
        }

    }
}